﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FormsAuthenticationExtensions;


using System.Text.RegularExpressions;
using Subplicity.Models;
using System.Web.Security;



namespace Subplicity.Controllers
{
    public class UserController : BaseController
    {
       
        [HttpPost]
        [AllowAnonymous]
        public ActionResult Login(string loginUserName, string loginPassword)
        {
            string result = string.Empty;

            CheckUserLogin(loginUserName, loginPassword);
            if (ViewBag.Me != null)
            {
                Authorize(((StaffService.Staff)ViewBag.Me).ID.Value);
               
                result = new JsonObject().ForResponse(true, "");
               

            }
            else
                result = new JsonObject().ForResponse(true, "");

               

            return new JsonResult() { Data = result };
            
        }

    
        [AllowAnonymous]
        public ActionResult SignOut()
        {
            Session.RemoveAll();

           FormsAuthentication.SignOut();

            me = null;
            ViewBag.me = null;

            return Redirect("/");
        }

        private void Authorize(long staffID)
        {
            
            //SET login credential
            var ticketData = new NameValueCollection { { "StaffID", staffID.ToString() } };
        
           new  FormsAuthentication().SetAuthCookie(staffID.ToString(), true, ticketData);
           
          
             
        }

    }
}
